package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constants.BaseConstants;
import cn.itsource.ymcc.domain.CourseMarket;
import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.mapper.CourseUserLearnMapper;
import cn.itsource.ymcc.service.ICourseMarketService;
import cn.itsource.ymcc.service.ICourseUserLearnService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2021-09-18
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private CourseUserLearnMapper courseUserLearnMapper;


    @Autowired
    private ICourseMarketService courseMarketService;

    @Override
    public void create(Long userId, Long courseId, String orderNo) {
        CourseUserLearn lean = new CourseUserLearn();
        lean.setCourseId(courseId);
        lean.setLoginId(userId);
        lean.setCourseOrderNo(orderNo);

        CourseMarket courseMarket = courseMarketService.selectById(courseId);
        lean.setEndTime(DateUtils.addDays(new Date(),courseMarket.getValidDays()));
        lean.setStartTime(new Date());
        lean.setState(BaseConstants.CourseLean.STATE_NORMAL);
        insert(lean);
    }

    @Override
    public CourseUserLearn selectByCourseOrderNo(String outTradeNo) {
        return courseUserLearnMapper.selectByCourseOrderNo( outTradeNo);
    }
}
